<template>
  <div class="loginregistbox">
    <ul>
      <li>
        <div class="crbottomcontent">
          <input
            type="text"
            placeholder="请输入账号"
            class="user"
            v-model="loginForm.account"
            @blur="blurAccount"
            @focus="focusAccount"
            ref="inputAccount"
          />
          <br />
          <p class="pzh" ref="hiddenAccount">请输入账号</p>
          <div class="pwdeyebox">
            <input
              type="password"
              placeholder="密码"
              class="pwd"
              v-model="loginForm.password"
              @blur="blurPassword"
              ref="inputPwd"
              @focus="focusPassword"
            /><br />
            <img
              :src="imgUrl"
              alt=""
              class="eye"
              ref="eyes"
              @click="changeType"
            />
          </div>
          <p class="pmm" ref="hiddenPassword">请输入密码</p>

          <input
            type="checkbox"
            class="rememberpwd"
            v-model="loginForm.remember"
            @click="rememberPwd"
          />记住密码

          <br />

          <button @click.prevent="login">登录</button><br />
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "ManageLogin",
  data() {
    return {
      loginForm: {
        account: "",
        password: "",
        remember: false,
        UserStatus: 0,
      },
      flag: true,
      imgUrl: require("../../assets/close.png"),
    };
  },
  methods: {
    blurAccount() {
      if (!this.loginForm.account) {
        this.$refs.inputAccount.className = "change2 user";
        this.$refs.hiddenAccount.style.display = "block";
      } else this.$refs.hiddenAccount.style.display = "none";
    },
    focusAccount() {
      this.$refs.inputAccount.className = "change1 user";
    },

    blurPassword() {
      // console.log(this.$refs.pmm);
      if (!this.loginForm.password) {
        this.$refs.inputPwd.className = "change2 pwd";
        this.$refs.hiddenPassword.style.display = "block";
      } else this.$refs.hiddenPassword.style.display = "none";
    },

    focusPassword() {
      this.$refs.inputPwd.className = "change1 pwd";
    },

    // 记住密码
    rememberPwd() {
      this.loginForm.remember = !this.loginForm.remember;
      // 记住账号
      sessionStorage.setItem("account", JSON.stringify(this.loginForm.account));
      if (this.loginForm.remember) {
        //   记住密码
        sessionStorage.setItem(
          "password",
          JSON.stringify(this.loginForm.password)
        );
      } else {
        sessionStorage.removeItem("account");
        sessionStorage.removeItem("password");
      }
    },

    // 登录按钮
    login() {
      if (this.loginForm.account && this.loginForm.password) {
        //  验证邮箱
        const regEmail =
          /^[A-Za-z0-9]+([_.][A-Za-z0-9]+)*@([A-Za-z0-9-]+\.)+[A-Za-z]{2,6}$/;
        //   验证手机号
        const regMobile = /^1[34578]\d{9}$/;
        if (
          regEmail.test(this.loginForm.account) ||
          regMobile.test(this.loginForm.account)
        ) {
          // 连接服务器
          this.$http({
            method: "post",
            // headers: {
            //   "content-Type": "application/json",
            // },
            url: "admin/login",
            params: {
              aname: this.loginForm.account,
              apassword: this.loginForm.password,
            },
          }).then(({ data }) => {
            console.log("123456", data);
            if (data.flag) {
              // 登录成功进行路由跳转
              console.log("登录成功");
              this.$message.success("登录成功");
              // 跳转到最高管理员页面
              this.$router.push({
                name: "BackgroundManage",
                // name:"TopAdministrator"
              });
              //     // 获取所有登录信息
              this.$store.commit("LoginxMessage", {
                message: data.data.admin,
              });
              //存入token到vuex中
              this.$store.commit("LoginxToken", {
                token: data.data.token,
              });
              this.loginForm.UserStatus = 1;
              // 名称
              this.$store.commit("Loginx", {
                UserName: data.data.admin.username,
              });
              //  登录状态
              this.$store.commit("LoginxStatus", {
                UserStatus: this.loginForm.UserStatus,
              });
            } else {
              // 登录失败展示错误信息
              console.log("登录失败");
              this.$message.error(data.errorMsg);
            }
          });
        } else {
          alert("账号格式错误,请重新输入");
        }
      } else {
        if (!this.loginForm.account || !this.loginForm.password) {
          alert("请输入账号密码");
        }
      }
    },

    // 密码的显示与隐藏
    changeType() {
      console.log(this.$refs);
      if (this.flag) {
        this.$refs.inputPwd.type = "text";
        this.imgUrl = require("../../assets/open.png");
        this.flag = false;
      } else {
        this.$refs.inputPwd.type = "password";
        this.imgUrl = require("../../assets/close.png");
        this.flag = true;
      }
    },
  },
  mounted() {
    // 记住密码
    // 提取上次登录得账号
    this.$set(
      this.loginForm,
      "account",
      JSON.parse(sessionStorage.getItem("account"))
    );
    // 如果密码字段在本地存在就提取出来
    if (sessionStorage.getItem("password") != null) {
      this.$set(
        this.loginForm,
        "password",
        JSON.parse(sessionStorage.getItem("password"))
      );
    }
  },
};
</script>

<style scoped>
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
li {
  list-style: none;
}
a {
  text-decoration: none !important;
}
.loginregistbox ul {
  width: 900px;
}

.loginregistbox ul li {
  float: left;
}
.loginregistbox ul li:last-child {
  margin-left: 20px;
}

.contentrightbottom {
  /* position: relative;
    z-index: 99; */
  /* background:url(../assets/newbg.png); */
  background-repeat: no-repeat;
  width: 100%;
  /* background-color: #F9F9F9; */
}
.contentrightbottombox {
  opacity: 0.9;
  position: relative;
  overflow: hidden;
  width: 450px;
  height: 500px;
  margin-top: 100px;
  margin-left: 400px;
  border-radius: 5px;
  background-color: #fff;
}
.crbottomlogin {
  width: 360px;
  height: 41px;
  line-height: 31px;
  margin-top: 50px;
  margin-left: 40px;
  border: none;
}
.crbottomlogin .login {
  float: left;
  border-bottom: 4px solid skyblue;
}
.crbottomlogin .regist {
  float: left;
  margin-left: 10px;

  /* border-bottom: 4px solid skyblue; */
}
.crbottomlogin .login a,
.crbottomlogin .regist a {
  font-size: 22px;
  color: #333333;
}

.crbottomcontent .user {
  position: relative;
  z-index: 5;
  border: none;
  outline: none;
  width: 354px;
  height: 60px;
  margin-left: 40px;
  font-size: 17px;
  background-color: #f9f9f9;
  margin-top: 10px;
  padding-left: 20px;
}
.pzh {
  display: none;
  color: red;
  font-size: 12px;
  margin-top: 10px;
  margin-left: 40px;
  margin-bottom: 0px;
}
.pmm {
  display: none;
  color: red;
  font-size: 12px;
  margin-top: 10px;
  margin-left: 40px;
  margin-bottom: 0px;
}

.crbottomcontent .pwd {
  position: relative;
  border: none;
  margin-top: 10px;
  padding-left: 20px;
  margin-bottom: 10px;
  outline: none;
  font-size: 17px;
  width: 354px;
  height: 60px;
  margin-left: 40px;
  background-color: #f9f9f9;
}

.rememberpwd {
  margin-left: 80px;

  vertical-align: middle;
}

.crbottomcontent .checks {
  margin-top: 35px;
  width: 16px;
  height: 16px;
  vertical-align: bottom;
  margin-left: 60px;
}
.crbottomcontent span {
  font-size: 14px;
  color: #c1c1c1;
}
.crbottomcontent button {
  border: none;
  margin-top: 25px;
  margin-left: 40px;
  margin-bottom: 20px;
  width: 354px;
  height: 60px;
  font-size: 17px;
  color: #fff;
  font-weight: 600;
  background-color: skyblue;
}

.identity {
  width: 460px;
  height: 75px;

  margin-top: 20px;
  line-height: 75px;
}
.identity span:first-child {
  margin-left: 140px;
  border: 1px solid #c1c1c1;
  display: inline-block;
  text-align: center;
  line-height: 20px;
  width: 50px;
  height: 25px;
}
.identity span:last-child {
  margin-left: 80px;
  border: 1px solid #c1c1c1;
  display: inline-block;
  text-align: center;
  line-height: 20px;
  width: 60px;
  height: 25px;
}

.phonelogin {
  display: inline-block;
  margin-left: 180px;
  margin-top: 10px;
}
.phonelogin a,
.crbottomcontent .other {
  margin-top: 400px;
  margin-left: 120px;
  font-size: 17px;
  color: #aaaaaa;
  border-bottom: 1px solid #aaaaaa;
}
.crbottomcontent .other:hover {
  color: skyblue;
  border-color: skyblue;
}
.crbottomcontentimg span img {
  height: 46px;
  width: 46px;
}
.crbottomcontentimg span:first-child {
  margin-left: 100px;
}

.conpany {
  margin-top: 20px;
  margin-left: 440px;
  font-size: 12px;
  color: #999999;
}

.change1::placeholder {
  position: absolute;
  top: 5px;
  left: 20px;
  font-size: 12px;
  color: #c1c1c1;
  transition: all 0.3s;
}
.change2::placeholder {
  font-size: 17px;
  color: red;
  transition: all 0.3s;
}

.pwdeyebox {
  position: relative;
}

.eye {
  position: absolute;
  z-index: 50;
  top: 20px;
  right: 15px;
  width: 30px;
  height: 30px;
}
</style>